<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SpyEmbankments Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="./files/js/jquery.js" type="text/javascript" language="javascript"></script>
<link rel="stylesheet" type="text/css" href="./style/login.css" />

<script language="javascript">
//function getUrlVars()
//{
//    var vars = [], hash;
//    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
//    for(var i = 0; i < hashes.length; i++)
//    {
//        hash = hashes[i].split('=');
//        vars.push(hash[0]);
//        vars[hash[0]] = hash[1];
//    }
//    return vars;
//}
//alert(getUrlVars()["first"]);
//if(!getUrlVars()["first"])
//{
//    self.location=document.URL+"?&first=true";
//}
$(document).ready(function()
{
	$("#login_form").submit(function()
	{
		//rimuovo tutte tutte le classi aggiungo le classi messagebox e inizio il fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//controllo se lo user esiste o no da ajax
		$.post("./files/php/ajax_login.php",{ user_name:$('#username').val(),
                                                      password:$('#password').val(),
                                                      rand:Math.random() } ,
                function(data)
                {
		  if(data>0) 
		  {
                    $("#msgbox").fadeTo(200,0.1,function()  //inizio il messagebox fading
                    { 
                    //inserisco il messaggio di Logging in... e cambio la classe del box in messageok e riparto con il fading
                    $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                    function()
                    { 
                         //redirect to secure page
                         if(data== 1) //admin
                         {
                             var url=$("#url").val();
                             document.location=url;
                         }
                         else
                         {
                             document.location="Home.php";
                         }
                         
                    });

                    });
                 }
                 else 
                 {
                    $("#msgbox").fadeTo(200,0.1,function() //inizio il fading della messagebox
                    { 
                    //inserisco il di Login sbagliato e cambio la classe del box in messageerror e riparto con il fading
                    $(this).html('User e password inserite non corrette...').addClass('messageboxerror').fadeTo(900,1);
                    });		
                 }		
        });
 		return false; 
	});
	//ora chiamo the ajax  e e triggero il focus 
	$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});
});
</script>

</head>
<body>
<?php
if(preg_match_all('/EliminaDati/', $_GET["url"]))
    print '<p><b>Per eliminare i dati dal data base &egrave; richiesto il login come amministratore del sistema</b></p>';

?>
 
<form method="post" action="" id="login_form">
	<fieldset>
		<legend>Log in</legend>
                
		<label for="user">User Name : </label> <input name="username" type="text" id="username" value="" maxlength="20" />

		<div class="clear"></div>
                
		<label for="password">Password : </label> <input name="password" type="password" id="password" value="" maxlength="20" />
		<div class="clear"></div>
<?php
                if($_GET["url"]!="")
                    print '<input name="url" type="hidden" id="url" value="'.$_GET["url"].'" />';
                else
                    print '<input name="url" type="hidden" id="url" value="Home.php" />';
                        
?>
		<input name="Submit" type="submit" id="submit" value="Login" style="margin: 0px 0 0 287px;" class="button" /> <span id="msgbox" style="display:none"></span>

	</fieldset>
</form>
</body>
</html>
